package com.property.view;

import java.io.File;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.text.AbstractDocument.Content;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.springframework.web.servlet.view.document.AbstractExcelView;

import com.property.entity.Complaint;
import com.property.entity.Owner;

public class ExcelView extends AbstractExcelView{

	@Override
	protected void buildExcelDocument(Map<String, Object> model,
			HSSFWorkbook workbook, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		List<Owner> list = (List<Owner>) model.get("comList");
		if(list != null && list.size() != 0){
			int len = list.size();
			Sheet sheet=workbook.createSheet();
			
			Row row=sheet.createRow(0);
			Cell cell=row.createCell(0, Cell.CELL_TYPE_STRING);
			cell.setCellValue("编号");
			cell=row.createCell(1,Cell.CELL_TYPE_STRING);
			cell.setCellValue("用户昵称");
			cell=row.createCell(2,Cell.CELL_TYPE_STRING);
			cell.setCellValue("业主姓名");
			cell=row.createCell(3, Cell.CELL_TYPE_STRING);
			cell.setCellValue("联系电话");
			cell=row.createCell(4,Cell.CELL_TYPE_STRING);
			cell.setCellValue("所在楼栋");
			cell=row.createCell(5,Cell.CELL_TYPE_STRING);
			cell.setCellValue("所在房屋");
			cell=row.createCell(6,Cell.CELL_TYPE_STRING);
			cell.setCellValue("状态");
			
			
			for(int i=0;i<len;i++){
				row =sheet.createRow(i+1);
				cell=row.createCell(0, Cell.CELL_TYPE_STRING);
				cell.setCellValue(list.get(i).getId());
				cell=row.createCell(1,Cell.CELL_TYPE_STRING);
				cell.setCellValue(list.get(i).getUser().getUserName());
				cell=row.createCell(2,Cell.CELL_TYPE_STRING);
				cell.setCellValue(list.get(i).getName());
				cell=row.createCell(3,Cell.CELL_TYPE_STRING);
				cell.setCellValue(list.get(i).getPhone());
				cell=row.createCell(4, Cell.CELL_TYPE_STRING);
				cell.setCellValue(list.get(i).getTower());
				cell=row.createCell(5, Cell.CELL_TYPE_STRING);
				cell.setCellValue(list.get(i).getHouseIN());
				cell=row.createCell(6, Cell.CELL_TYPE_STRING);
				cell.setCellValue(list.get(i).getStatus());
			}
		}
		response.setContentType("application/vnd.ms-excel");
		response.setCharacterEncoding("utf-8");
		String excelName="业主房屋信息表.xls";
		response.setHeader("Content-disposition", "attachment;filename="+new String(excelName.getBytes("gbk"),"ISO-8859-1"));
		OutputStream os=response.getOutputStream();
		workbook.write(os);
		os.flush();
		os.close();
	}

}
